package com.example.administrator.testc.Emoji;/**
 * Created by Administrator on 2016/5/5 0005.
 */

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.example.administrator.testc.Emoji.util.ParseEmojiMsgUtil;
import com.example.administrator.testc.Emoji.util.SelectFaceHelper;
import com.example.administrator.testc.R;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class TestMain extends Activity implements View.OnClickListener{

    private static final String TAG = TestMain.class.getSimpleName();

    private SelectFaceHelper mFaceHelper;
    private View addFaceToolView;//表情viewpager视图
    private boolean isVisbilityFace;
    private EditText mEditMessageEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emoji_layout);

        findViewById(R.id.btnSend).setOnClickListener(this);
        findViewById(R.id.btn_to_face).setOnClickListener(this);
        mEditMessageEt = (EditText) findViewById(R.id.txtMessage);
        addFaceToolView = (View) findViewById(R.id.add_tool);
        mEditMessageEt.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isVisbilityFace = false;
                addFaceToolView.setVisibility(View.GONE);
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_to_face:
                if (null == mFaceHelper) {
                    mFaceHelper = new SelectFaceHelper(TestMain.this, addFaceToolView);
                    mFaceHelper.setFaceOpreateListener(new SelectFaceHelper.OnFaceOprateListener() {
                        @Override
                        public void onFaceSelected(SpannableString spanEmojiStr) {
                            if (null != spanEmojiStr) {
                                mEditMessageEt.append(spanEmojiStr);
                            }
                        }

                        @Override
                        public void onFaceDeleted() {
                            int selection = mEditMessageEt.getSelectionStart();
                            String text = mEditMessageEt.getText().toString();
                            if (selection > 0) {
                                String text2 = text.substring(selection - 1);
                                if ("]".equals(text2)) {
                                    int start = text.lastIndexOf("[");
                                    int end = selection;
                                    mEditMessageEt.getText().delete(start, end);
                                    return;
                                }
                                mEditMessageEt.getText().delete(selection - 1, selection);
                            }
                        }

                    });
                }
                if (isVisbilityFace) {
                    isVisbilityFace = false;
                    addFaceToolView.setVisibility(View.GONE);
                } else {
                    isVisbilityFace = true;
                    addFaceToolView.setVisibility(View.VISIBLE);
                    hideInputManager(TestMain.this);
                }
                break;
            case R.id.btnSend:
                String msg = mEditMessageEt.getText().toString();
                if (msg.equals("")) {
                    return;
                }
                String msgStr = ParseEmojiMsgUtil.convertToMsg(mEditMessageEt.getText(), TestMain.this);// 这里不要直接用mEditMessageEt.getText().toString();
                Log.i("TAGSS", "msgStr is " + msgStr);
                mEditMessageEt.setText("");
                break;
            default:
                break;
        }
    }

    public void onBackPressed() {
        if (isVisbilityFace) {// 好吧,隐藏表情菜单再退出
            isVisbilityFace = false;
            addFaceToolView.setVisibility(View.GONE);
            return;
        }
        finish();
    };

    // 隐藏软键盘
    public void hideInputManager(Context ct) {
        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(((Activity) ct)
                    .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            Log.e(TAG, "hideInputManager Catch error,skip it!", e);
        }
    }
}